﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace JoomlaXMLEditor
{
    // edytor pliku
    public partial class TemplateFileEditor : Form
    {
        // uchwyt do okna
        private TemplateFiles parent;
        // indeks przy edycji
        private int finalIndex;
        /// <summary>
        /// konstruktor
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="index"></param>
        public TemplateFileEditor(TemplateFiles parent, int index)
        {
            InitializeComponent();

            this.parent = parent;
            this.finalIndex = index;

            if (index != -1)
            {
                this.textBox1.Text = this.parent.listBox1.Items[index].ToString();
                this.comboBox1.Text = this.parent.files[index].Name;
                this.comboBox1.Enabled = false;
            }
        }
        // zamykanie
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        // zapisywanie
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text != "" && this.comboBox1.Text != "")
            {
                if (finalIndex != -1)
                {
                    this.parent.listBox1.Items[finalIndex] = this.textBox1.Text;
                    this.parent.files[finalIndex].FirstChild.Value = this.textBox1.Text;
                }
                else
                {
                    this.parent.listBox1.Items.Add(this.textBox1.Text);
                    XmlElement node = this.parent.parent.actualFile.CreateElement(this.comboBox1.Text);
                    XmlText textNode = this.parent.parent.actualFile.CreateTextNode(this.textBox1.Text);
                    node.AppendChild(textNode);
                    this.parent.files.Add(node);
                }

                this.Close();
            }
            else
            {
                MessageBox.Show("Proszę sprawdzić poprawność wypełnienia formularza");
            }
        }
    }
}
